package ATM;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;


public class ATMFrame extends JFrame {
    private final String FILE_PATH = "src/ATM/atm.user";
    private CardLayout cardLayout = new CardLayout();
    private Image bgImg = new ImageIcon("src/ATM/image/background.jpg").getImage();
    private ArrayList<User> users = new ArrayList<>();
    private int index = -1;
    private LoginPanel loginPanel = new LoginPanel(this);
    private MenuPanel menuPanel = new MenuPanel(this);
    private GetPanel getPanel = new GetPanel(this);
    private ChangePwdPanel changePwdPanel = new ChangePwdPanel(this);
    private SavePanel savePanel = new SavePanel(this);
    private SearchPanel searchPanel = new SearchPanel(this);
    private TransferPanel transferPanel = new TransferPanel(this);


    public ATMFrame(){

        init();
        this.setContentPane(new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(bgImg,0,0,this);
            }
        });


        this.setLayout(cardLayout);

        this.add(loginPanel,"login");
        this.add(menuPanel,"menu");
        this.add(changePwdPanel,"changePwd");
        this.add(getPanel,"get");
        this.add(savePanel,"save");
        this.add(searchPanel,"search");
        this.add(transferPanel,"transfer");


        this.setTitle("欢迎使用中国银行ATM");
        this.setSize(1024, 768);
        this.setLocationRelativeTo(null);
        this.setResizable(false);
//        this.setUndecorated(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    public void init(){
        users = (ArrayList<User>) FileUtil.readObject(FILE_PATH);
        if (users == null){
            users.add(new User("aaa","111",1000));
            users.add(new User("bbb","222",2000));
            users.add(new User("ccc","333",3000));
            users.add(new User("ddd","444",4000));
            saveUser();
        }
    }



    public void saveUser(){
        FileUtil.writeObject(users,FILE_PATH);
    }


    public void showPanel(String name){
        cardLayout.show(this.getContentPane(),name);
    }

    public ArrayList<User> getUsers() {
        return users;
    }

    public void setUsers(ArrayList<User> users) {
        this.users = users;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getFILE_PATH() {
        return FILE_PATH;
    }

    public SearchPanel getSearchPanel() {
        return searchPanel;
    }

    public GetPanel getGetPanel() {
        return getPanel;
    }

    public ChangePwdPanel getChangePwdPanel() {
        return changePwdPanel;
    }

    public SavePanel getSavePanel() {
        return savePanel;
    }

    public TransferPanel getTransferPanel() {
        return transferPanel;
    }

    public LoginPanel getLoginPanel() {
        return loginPanel;
    }
}
